package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.POJO.Order;
import com.itheima.service.orderService;
import com.itheima.utils.SMSUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 体检预约服务
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Reference
    private orderService orderService;

    //新增体检预约信息
    @RequestMapping("/addOrderAndMember")
    public Result addOrderAndMember(@RequestBody Map map,Integer[] checkgroupIds) {
        try {
            map.put("orderType", Order.ORDERTYPE_TELEPHONE);
            Result result = orderService.addOrderAndMember(map, checkgroupIds);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDER_FAIL);
        }
    }

    //取消预约(删除预约信息)
    @RequestMapping("/deleteOrderById")
    public Result deleteOrderById(Integer id) {
        try {
            orderService.deleteOrderById(id);
            return new Result(true,MessageConstant.DELETE_ORDER_SUCCESS);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ORDER_FAIL);
        }
    }

    //确定预约发送短信提示
    @RequestMapping("/sendMessage2Confirm")
    public Result sendMessage2Confirm(String telephone,String orderDate) {
        try {
            SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,telephone,orderDate);
            return new Result(true,MessageConstant.SEND_ORDER_NOTICE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SEND_ORDER_NOTICE_FAIL);
        }
    }

    //分页查询体检预约信息
    @RequestMapping("/pageQuery")
    public Result pageQuery(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = orderService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }

    }


}
